package com.dykj.fireFighting.web.devicemanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.DeviceInfoDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.DeviceInfo;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceInfoService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.PageVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * VIEW接口
 *
 * @Author JacksonTu
 * @Date  2021-06-29 13:26:48
 */
@Slf4j
@Api(value = "VIEW接口", tags = {"VIEW接口"})
@RestController
@RequestMapping("/deviceInfo")
public class DeviceInfoController {

    @DubboReference
    private IDeviceInfoService deviceInfoService;


    /**
     * 消防群组弹窗
     *
     * @return
     */
    @AutoLog(value = "消防群组弹窗")
    @ApiOperation(value = "消防群组弹窗", notes = "消防群组弹窗")
    @GetMapping("/list/{id}")
    public CommonResult<PageVo<DeviceInfo>> treeGrid(DeviceInfoDto commonDto, @PathVariable("id") String id) {
        commonDto.setTempId(id);
        PageVo<DeviceInfo> page = deviceInfoService.pageList(commonDto);
        log.info("设备查询结果:{}",page.toString());
        return CommonResult.success(page);
    }

    @AutoLog(value = "消防站弹窗")
    @ApiOperation(value = "消防站弹窗", notes = "消防站弹窗")
    @GetMapping("/fireStation/list/{id}")
    public CommonResult<PageVo<DeviceInfo>> fireStationList(DeviceInfoDto commonDto, @PathVariable("id") String id) {
        commonDto.setTempId(id);
        PageVo<DeviceInfo> page = deviceInfoService.pageListFireStation(commonDto);
        log.info("消防站设备查询结果:{}",page.toString());
        return CommonResult.success(page);
    }

    @AutoLog(value = "危险源弹窗")
    @ApiOperation(value = "危险源弹窗", notes = "危险源弹窗")
    @GetMapping("/hazardSources/list/{id}")
    public CommonResult<PageVo<DeviceInfo>> hazardSourcesList(DeviceInfoDto commonDto, @PathVariable("id") String id) {
        commonDto.setTempId(id);
        PageVo<DeviceInfo> page = deviceInfoService.pageListHazardSources(commonDto);
        log.info("危险源设备查询结果:{}",page.toString());
        return CommonResult.success(page);
    }

    @AutoLog(value = "重点部位弹窗")
    @ApiOperation(value = "重点部位弹窗", notes = "重点部位弹窗")
    @GetMapping("/keyParts/list/{id}")
    public CommonResult<PageVo<DeviceInfo>> keyPartsList(DeviceInfoDto commonDto, @PathVariable("id") String id) {
        commonDto.setTempId(id);
        PageVo<DeviceInfo> page = deviceInfoService.pageListKeyParts(commonDto);
        log.info("重点部位设备查询结果:{}",page.toString());
        return CommonResult.success(page);
    }


    /**
     * 获取VIEW信息
     */
    @AutoLog(value = "VIEW-获取VIEW信息")
    @ApiOperation(value = "获取VIEW信息", notes = "获取VIEW信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<DeviceInfo> info(@PathVariable("id") String id){
        DeviceInfo deviceInfo = deviceInfoService.getById(id);
        return CommonResult.success(deviceInfo);
    }

    /**
     * 保存VIEW信息
     */
    @AutoLog(value = "VIEW-保存VIEW信息")
    @ApiOperation(value = "保存VIEW信息", notes = "保存VIEW信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody DeviceInfo deviceInfo) {
        try {
            deviceInfo.setCreateTime(new Date());
                deviceInfoService.save(deviceInfo);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改VIEW信息
     */
    @AutoLog(value = "VIEW-修改VIEW信息")
    @ApiOperation(value = "修改VIEW信息", notes = "修改VIEW信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody DeviceInfo deviceInfo) {
        try {
                deviceInfoService.updateById(deviceInfo);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除VIEW信息
     */
    @AutoLog(value = "VIEW-删除VIEW信息")
    @ApiOperation(value = "删除VIEW信息", notes = "删除VIEW信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                deviceInfoService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
